<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
 * FCKeditor - The text editor for Internet - http://www.fckeditor.net
 * Copyright (C) 2003-2010 Frederico Caldeira Knabben
 *
 * == BEGIN LICENSE ==
 *
 * Licensed under the terms of any of the following licenses at your
 * choice:
 *
 *  - GNU General Public License Version 2 or later (the "GPL")
 *    http://www.gnu.org/licenses/gpl.html
 *
 *  - GNU Lesser General Public License Version 2.1 or later (the "LGPL")
 *    http://www.gnu.org/licenses/lgpl.html
 *
 *  - Mozilla Public License Version 1.1 or later (the "MPL")
 *    http://www.mozilla.org/MPL/MPL-1.1.html
 *
 * == END LICENSE ==
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
	<title>FCKW3CRange - Tests for JsUnit</title>
	<script src="../../../editor/_source/fckscriptloader.js" type="text/javascript"></script>
	<script type="text/javascript" src="tests.js"></script>
	<script type="text/javascript" src="../_jsunit/app/jsUnitCore.js"></script>
	<script type="text/javascript">

FCKScriptLoader.Load( 'FCKW3CRange' ) ;

	</script>
	<script type="text/javascript">

function test_CreateRange()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	assertNotNull( range ) ;
	assertNotUndefined( range.startContainer ) ;

	if ( range.TypeName == 'FCKW3CRange' )
		inform( 'FCKW3CRange is our custom implementation' ) ;
	else
		inform( 'FCKW3CRange is the default browser implementation' ) ;
}

function test_setStart()
{
	var range = FCKW3CRange.CreateRange( document ) ;
	range.setStart( document.body, 1 ) ;

	assertEquals( document.body, range.startContainer ) ;
	assertEquals( 1, range.startOffset ) ;
	assertEquals( document.body, range.endContainer ) ;
	assertEquals( 1, range.endOffset ) ;
	assertTrue( range.collapsed ) ;
}


function test_setEnd()
{
	var range = FCKW3CRange.CreateRange( document ) ;
	range.setEnd( document.body, 1 ) ;

	assertEquals( document.body, range.startContainer ) ;
	assertEquals( 1, range.startOffset ) ;
	assertEquals( document.body, range.endContainer ) ;
	assertEquals( 1, range.endOffset ) ;
	assertTrue( range.collapsed ) ;
}

function test_setStartAfter()
{
	var range = FCKW3CRange.CreateRange( document ) ;
	range.setStartAfter( document.getElementById( '_B' ) ) ;
	range.setStartAfter( document.getElementById( '_H1' ).firstChild ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_H1' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_setStartBefore()
{
	var range = FCKW3CRange.CreateRange( document ) ;
	range.setStartBefore( document.getElementById( '_B' ) ) ;
	range.setStartBefore( document.getElementById( '_H1' ).firstChild ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_H1' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_setEndAfter()
{
	var range = FCKW3CRange.CreateRange( document ) ;
	range.setEndAfter( document.getElementById( '_H1' ).firstChild ) ;
	range.setEndAfter( document.getElementById( '_B' ) ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_H1' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_setEndBefore()
{
	var range = FCKW3CRange.CreateRange( document ) ;
	range.setEndBefore( document.getElementById( '_H1' ).firstChild ) ;
	range.setEndBefore( document.getElementById( '_B' ) ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_H1' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_selectNodeContents_Element()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	// Test with an Element node.
	range.selectNodeContents( document.getElementById( '_P' ) ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_P' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 3, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_selectNodeContents_Text()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	// Test with a Text node.
	range.selectNodeContents( document.getElementById( '_P' ).firstChild ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_P' ).firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ).firstChild, range.endContainer ) ;
	assertEquals( 'range.endOffset', 8, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_collapse_ToStart()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.selectNodeContents( document.getElementById( '_P' ) ) ;
	range.collapse( true ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_P' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 0, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_collapse_ToEnd()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.selectNodeContents( document.getElementById( '_P' ) ) ;
	range.collapse( false ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_P' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 3, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 3, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_insertNode_ElementContents()
{
	var eNewNode = document.createElement( 'span' ) ;
	eNewNode.innerHTML = 'test_' ;

	var range = FCKW3CRange.CreateRange( document ) ;

	range.selectNodeContents( document.getElementById( '_B' ) ) ;
	range.insertNode( eNewNode ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_B' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_B' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_insertNode_ElementCollapsed()
{
	var eNewNode = document.createElement( 'span' ) ;
	eNewNode.innerHTML = 'test_' ;

	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStartBefore( document.getElementById( '_P' ) ) ;
	range.insertNode( eNewNode ) ;

	assertEquals( 'range.startContainer', document.body, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body, range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_insertNode_ElementNotCollapsed()
{
	var eNewNode = document.createElement( 'span' ) ;
	eNewNode.innerHTML = 'test_' ;

	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStartBefore( document.getElementById( '_P' ) ) ;
	range.setStartBefore( document.getElementById( '_H1' ) ) ;
	range.insertNode( eNewNode ) ;

	assertEquals( 'range.startContainer', document.body, range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body, range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_insertNode_DiffElements()
{
	var eNewNode = document.createElement( 'span' ) ;
	eNewNode.innerHTML = 'test_' ;

	var range = FCKW3CRange.CreateRange( document ) ;

	range.selectNodeContents( document.getElementById( '_P' ) ) ;

	range.setStart( document.getElementById( '_H1' ), 0 ) ;
	range.insertNode( eNewNode ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_H1' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 3, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;

	assertEquals( 'Start must be on new node', range.startContainer.childNodes[range.startOffset], eNewNode ) ;
}

function test_insertNode_TextCollapsed()
{
	var eNewNode = document.createElement( 'span' ) ;
	eNewNode.innerHTML = 'test_' ;

	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_H1' ).firstChild, 3 ) ;
	range.insertNode( eNewNode ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_H1' ).firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 3, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_H1' ).childNodes[2], range.endContainer ) ;
	assertEquals( 'range.endOffset', 0, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_insertNode_TextNotCollapsed()
{
	var eNewNode = document.createElement( 'span' ) ;
	eNewNode.innerHTML = 'test_' ;

	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_H1' ).firstChild, 3 ) ;
	range.setEnd( document.getElementById( '_H1' ).firstChild, 5 ) ;
	range.insertNode( eNewNode ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_H1' ).firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 3, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_H1' ).childNodes[2], range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_insertNode_Mixed()
{
	var eNewNode = document.createElement( 'span' ) ;
	eNewNode.innerHTML = 'test_' ;

	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_H1' ).firstChild, 0 ) ;
	range.setEnd( document.getElementById( '_P' ), 1 ) ;
	range.insertNode( eNewNode ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_H1' ).firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.6 - Example 1
function test_deleteContents_W3C_1()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_P' ).firstChild, 1 ) ;
	range.setEnd( document.getElementById( '_P' ), 2 ) ;

	range.deleteContents() ;

	assertEquals( 'HTML after deletion', 't text.', GetTestInnerHtml( document.getElementById( '_P' ) ) ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_P' ).firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ).firstChild, range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.6 - Example 2
function test_deleteContents_W3C_2()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_B' ).firstChild, 1 ) ;
	range.setEnd( document.getElementById( '_B' ).nextSibling, 1 ) ;

	range.deleteContents() ;

	assertEquals( 'this is <b id=_b>s</b>text.', GetTestInnerHtml( document.getElementById( '_P' ) ) ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_P' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 2, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.6 - Example 3
function test_deleteContents_W3C_3()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_B' ).previousSibling, 1 ) ;
	range.setEnd( document.getElementById( '_B' ).firstChild, 1 ) ;

	range.deleteContents() ;

	assertEquals( 't<b id=_b>ome</b> text.', GetTestInnerHtml( document.getElementById( '_P' ) ) ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_P' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.6 - Example 4
function test_deleteContents_W3C_4()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_H1' ).firstChild, 1 ) ;
	range.setEnd( document.body.lastChild.firstChild, 1 ) ;

	range.deleteContents() ;

	assertEquals( '<h1 id=_h1>f</h1><p>nother paragraph.</p>', GetTestInnerHtml( document.body ) ) ;

	assertEquals( 'range.startContainer', document.body, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body, range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_deleteContents_Other()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_H1' ), 0 ) ;
	range.setEnd( document.body.lastChild, 1 ) ;

	range.deleteContents() ;

	assertEquals( '<h1 id=_h1></h1><p></p>', GetTestInnerHtml( document.body ) ) ;

	assertEquals( 'range.startContainer', document.body, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body, range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_deleteContents_Other_2()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.body, 0 ) ;
	range.setEnd( document.body, 2 ) ;

	range.deleteContents() ;

	assertEquals( '<p>another paragraph.</p>', GetTestInnerHtml( document.body ) ) ;

	assertEquals( 'range.startContainer', document.body, range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body, range.endContainer ) ;
	assertEquals( 'range.endOffset', 0, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_deleteContents_Other_3()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.selectNodeContents( document.getElementById('_B') ) ;

	range.deleteContents() ;

	assertEquals( '', GetTestInnerHtml( document.getElementById('_B') ) ) ;

	assertEquals( 'range.startContainer', document.getElementById('_B'), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById('_B'), range.endContainer ) ;
	assertEquals( 'range.endOffset', 0, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_deleteContents_Other_4()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.selectNodeContents( document.getElementById('_P') ) ;

	range.deleteContents() ;

	assertEquals( '', GetTestInnerHtml( document.getElementById('_P') ) ) ;

	assertEquals( 'range.startContainer', document.getElementById('_P'), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById('_P'), range.endContainer ) ;
	assertEquals( 'range.endOffset', 0, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.7 - Example 1
function test_extractContents_W3C_1()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_P' ).firstChild, 1 ) ;
	range.setEnd( document.getElementById( '_P' ), 2 ) ;

	var docFrag = range.extractContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Extracted HTML', 'his is <b id=_b>some</b>', GetTestInnerHtml( tmpDiv ) ) ;
	assertEquals( 'HTML after extraction', 't text.', GetTestInnerHtml( document.getElementById( '_P' ) ) ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_P' ).firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ).firstChild, range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.7 - Example 2
function test_extractContents_W3C_2()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_B' ).firstChild, 1 ) ;
	range.setEnd( document.getElementById( '_B' ).nextSibling, 2 ) ;

	var docFrag = range.extractContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Extracted HTML', '<b id=_b>ome</b> t', GetTestInnerHtml( tmpDiv ) ) ;
	assertEquals( 'HTML after extraction', 'this is <b id=_b>s</b>ext.', GetTestInnerHtml( document.getElementById( '_P' ) ) ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_P' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 2, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.6 - Example 3
function test_extractContents_W3C_3()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_B' ).previousSibling, 1 ) ;
	range.setEnd( document.getElementById( '_B' ).firstChild, 1 ) ;

	var docFrag = range.extractContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Extracted HTML', 'his is <b id=_b>s</b>', GetTestInnerHtml( tmpDiv ) ) ;
	assertEquals( 'HTML after extraction', 't<b id=_b>ome</b> text.', GetTestInnerHtml( document.getElementById( '_P' ) ) ) ;

	assertEquals( 'range.startContainer', document.getElementById( '_P' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.6 - Example 4
function test_extractContents_W3C_4()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_H1' ).firstChild, 1 ) ;
	range.setEnd( document.body.lastChild.firstChild, 1 ) ;

	var docFrag = range.extractContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Extracted HTML', '<h1 id=_h1>ckw3crange test</h1><p id=_p>this is <b id=_b>some</b> text.</p><p>a</p>', GetTestInnerHtml( tmpDiv ) ) ;
	assertEquals( '<h1 id=_h1>f</h1><p>nother paragraph.</p>', GetTestInnerHtml( document.body ) ) ;

	assertEquals( 'range.startContainer', document.body, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body, range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_extractContents_Other()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_H1' ), 0 ) ;
	range.setEnd( document.body.lastChild, 1 ) ;

	var docFrag = range.extractContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Extracted HTML', '<h1 id=_h1>fckw3crange test</h1><p id=_p>this is <b id=_b>some</b> text.</p><p>another paragraph.</p>', GetTestInnerHtml( tmpDiv ) ) ;
	assertEquals( '<h1 id=_h1></h1><p></p>', GetTestInnerHtml( document.body ) ) ;

	assertEquals( 'range.startContainer', document.body, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body, range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_extractContents_Other_2()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.body, 0 ) ;
	range.setEnd( document.body, 2 ) ;

	var docFrag = range.extractContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Extracted HTML', '<h1 id=_h1>fckw3crange test</h1><p id=_p>this is <b id=_b>some</b> text.</p>', GetTestInnerHtml( tmpDiv ) ) ;
	assertEquals( '<p>another paragraph.</p>', document.body.innerHTML.replace( /id=_H1/, 'id="_H1"' ).toLowerCase().replace( /\n|\r/g, '' ) ) ;

	assertEquals( 'range.startContainer', document.body, range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body, range.endContainer ) ;
	assertEquals( 'range.endOffset', 0, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_extractContents_Other_3()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.selectNodeContents( document.getElementById('_B') ) ;

	var docFrag = range.extractContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Extracted HTML', 'some', GetTestInnerHtml( tmpDiv ) ) ;
	assertEquals( 'HTML after extraction', '', GetTestInnerHtml( document.getElementById('_B') ) ) ;

	assertEquals( 'range.startContainer', document.getElementById('_B'), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById('_B'), range.endContainer ) ;
	assertEquals( 'range.endOffset', 0, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_extractContents_Other_4()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.selectNodeContents( document.getElementById('_P') ) ;

	var docFrag = range.extractContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Extracted HTML', 'this is <b id=_b>some</b> text.', GetTestInnerHtml( tmpDiv ) ) ;
	assertEquals( 'HTML after extraction', '', GetTestInnerHtml( document.getElementById('_P') ) ) ;

	assertEquals( 'range.startContainer', document.getElementById('_P'), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById('_P'), range.endContainer ) ;
	assertEquals( 'range.endOffset', 0, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_extractContents_Other_5()
{
	document.body.innerHTML = '<p><b><i>test</i></b></p>' ;

	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStartAfter( document.getElementsByTagName('i')[0] ) ;
	range.setEndAfter( document.getElementsByTagName('b')[0] ) ;

	var docFrag = range.extractContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Extracted HTML', '<b></b>', GetTestInnerHtml( tmpDiv ) ) ;
	assertEquals( 'HTML after extraction', '<p><b><i>test</i></b></p>', GetTestInnerHtml( document.body ) ) ;

	assertEquals( 'range.startContainer', document.body.firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body.firstChild, range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

function test_extractContents_Other_6()
{
	document.body.innerHTML = '<p><b><i>test</i></b></p>' ;

	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStartBefore( document.getElementsByTagName('b')[0] ) ;
	range.setEndBefore( document.getElementsByTagName('i')[0] ) ;

	var docFrag = range.extractContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Extracted HTML', '<b></b>', GetTestInnerHtml( tmpDiv ) ) ;
	assertEquals( 'HTML after extraction', '<p><b><i>test</i></b></p>', GetTestInnerHtml( document.body ) ) ;

	assertEquals( 'range.startContainer', document.body.firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body.firstChild, range.endContainer ) ;
	assertEquals( 'range.endOffset', 0, range.endOffset ) ;
	assertTrue( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.7 - Example 1
function test_cloneContents_W3C_1()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_P' ).firstChild, 1 ) ;
	range.setEnd( document.getElementById( '_P' ), 2 ) ;

	var bodyHtml = document.body.innerHTML ;

	var docFrag = range.cloneContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Cloned HTML', 'his is <b id=_b>some</b>', GetTestInnerHtml( tmpDiv ) ) ;

	// The body HTML must remain unchanged.
	assertEquals( bodyHtml, document.body.innerHTML ) ;

	// The range must also remain unchanged.
	assertEquals( 'range.startContainer', document.getElementById( '_P' ).firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_P' ), range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.7 - Example 2
function test_cloneContents_W3C_2()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_B' ).firstChild, 1 ) ;
	range.setEnd( document.getElementById( '_B' ).nextSibling, 2 ) ;

	var bodyHtml = document.body.innerHTML ;

	var docFrag = range.cloneContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Cloned HTML', '<b id=_b>ome</b> t', GetTestInnerHtml( tmpDiv ) ) ;

	// The body HTML must remain unchanged.
	assertEquals( bodyHtml, document.body.innerHTML ) ;

	// The range must also remain unchanged.
	assertEquals( 'range.startContainer', document.getElementById( '_B' ).firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_B' ).nextSibling, range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.6 - Example 3
function test_cloneContents_W3C_3()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_B' ).previousSibling, 1 ) ;
	range.setEnd( document.getElementById( '_B' ).firstChild, 1 ) ;

	var bodyHtml = document.body.innerHTML ;

	var docFrag = range.cloneContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Cloned HTML', 'his is <b id=_b>s</b>', GetTestInnerHtml( tmpDiv ) ) ;

	// The body HTML must remain unchanged.
	assertEquals( bodyHtml, document.body.innerHTML ) ;

	// The range must also remain unchanged.
	assertEquals( 'range.startContainer', document.getElementById( '_B' ).previousSibling, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById( '_B' ).firstChild, range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

// W3C DOM Range Specs - Section 2.6 - Example 4
function test_cloneContents_W3C_4()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_H1' ).firstChild, 1 ) ;
	range.setEnd( document.body.lastChild.firstChild, 1 ) ;

	var bodyHtml = document.body.innerHTML ;

	var docFrag = range.cloneContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Cloned HTML', '<h1 id=_h1>ckw3crange test</h1><p id=_p>this is <b id=_b>some</b> text.</p><p>a</p>', GetTestInnerHtml( tmpDiv ) ) ;

	// The body HTML must remain unchanged.
	assertEquals( bodyHtml, document.body.innerHTML ) ;

	// The range must also remain unchanged.
	assertEquals( 'range.startContainer', document.getElementById( '_H1' ).firstChild, range.startContainer ) ;
	assertEquals( 'range.startOffset', 1, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body.lastChild.firstChild, range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_cloneContents_Other()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.getElementById( '_H1' ), 0 ) ;
	range.setEnd( document.body.lastChild, 1 ) ;

	var bodyHtml = document.body.innerHTML ;

	var docFrag = range.cloneContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Cloned HTML', '<h1 id=_h1>fckw3crange test</h1><p id=_p>this is <b id=_b>some</b> text.</p><p>another paragraph.</p>', GetTestInnerHtml( tmpDiv ) ) ;

	// The body HTML must remain unchanged.
	assertEquals( bodyHtml, document.body.innerHTML ) ;

	// The range must also remain unchanged.
	assertEquals( 'range.startContainer', document.getElementById( '_H1' ), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body.lastChild, range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_cloneContents_Other_2()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.setStart( document.body, 0 ) ;
	range.setEnd( document.body, 2 ) ;

	var bodyHtml = document.body.innerHTML ;

	var docFrag = range.cloneContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Cloned HTML', '<h1 id=_h1>fckw3crange test</h1><p id=_p>this is <b id=_b>some</b> text.</p>', GetTestInnerHtml( tmpDiv ) ) ;

	// The body HTML must remain unchanged.
	assertEquals( bodyHtml, document.body.innerHTML ) ;

	// The range must also remain unchanged.
	assertEquals( 'range.startContainer', document.body, range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.body, range.endContainer ) ;
	assertEquals( 'range.endOffset', 2, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_cloneContents_Other_3()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.selectNodeContents( document.getElementById('_B') ) ;

	var bodyHtml = document.body.innerHTML ;

	var docFrag = range.cloneContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Cloned HTML', 'some', GetTestInnerHtml( tmpDiv ) ) ;

	// The body HTML must remain unchanged.
	assertEquals( bodyHtml, document.body.innerHTML ) ;

	assertEquals( 'range.startContainer', document.getElementById('_B'), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById('_B'), range.endContainer ) ;
	assertEquals( 'range.endOffset', 1, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

function test_cloneContents_Other_4()
{
	var range = FCKW3CRange.CreateRange( document ) ;

	range.selectNodeContents( document.getElementById('_P') ) ;

	var bodyHtml = document.body.innerHTML ;

	var docFrag = range.cloneContents() ;

	var tmpDiv = document.createElement( 'div' ) ;
	if ( docFrag.AppendTo ) docFrag.AppendTo( tmpDiv ) ; else tmpDiv.appendChild( docFrag ) ;

	assertEquals( 'Cloned HTML', 'this is <b id=_b>some</b> text.', GetTestInnerHtml( tmpDiv ) ) ;

	// The body HTML must remain unchanged.
	assertEquals( bodyHtml, document.body.innerHTML ) ;

	assertEquals( 'range.startContainer', document.getElementById('_P'), range.startContainer ) ;
	assertEquals( 'range.startOffset', 0, range.startOffset ) ;
	assertEquals( 'range.endContainer', document.getElementById('_P'), range.endContainer ) ;
	assertEquals( 'range.endOffset', 3, range.endOffset ) ;
	assertFalse( 'range.collapsed', range.collapsed ) ;
}

	</script>
<script type="text/javascript">

var _BodyHtml ;

function setUpPage()
{
	// Get the trimmed innerHTML.
	_BodyHtml = document.body.innerHTML.replace( /^\s+|\s+$/g, '' ) ;
	setUpPageStatus = 'complete' ;
}

// JsUnit special function called before every test start.
function setUp()
{
	// Reset the body (because of changes by test functions).
	document.body.innerHTML = _BodyHtml ;
}

// Use window.onload to call a test outside JsUnit (for debugging).
// The "tests.js" script must be commented.
//window.onload = function()
//{
//	test_extractContents_Other_3() ;
//}

	</script>
</head>
<body><h1 id="_H1">FCKW3CRange Test</h1><p id="_P">This is <b id="_B">some</b> text.</p><p>Another paragraph.</p></body>
</html>
